// promise 封装

const ajax = (url) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.responseType = 'json';
        xhr.onload = function() {
            if (this.status === 200) {
                resolve(this.response);
            } else {
                reject(new Error(this.statusText));
            }
        }
        xhr.send();
    })
}

/*-------------------------------------------------------------------------------------------------------- */

//  链式调用
ajax('./res/user1.json').then((value) => {
    console.log(value);
    return ajax('./res/user2.json')
}).then(value => {
    console.log(value);
}).catch((err) => {
    console.log(err)
})

/*-------------------------------------------------------------------------------------------------------- */

//  生成器

function * foo () {
    console.log('start');
    const res = yield 'foo';
    console.log(res);
}

const generator = foo();
const result = generator.next()

console.log(result)

generator.next('bar')
/*-------------------------------------------------------------------------------------------------------- */